﻿#include "global.h"

QNetworkAccessManager g_networkManager;//网络请求管理对象
QSqlDatabase g_sqlDabase;//数据库对象
QMqttClient g_mqttClient;//mqtt通信对象
QString g_httpBaseUrl;//http基本url
QString g_mqttBaseUrl;//mqtt基本url
QString g_mqttUsername;//mqtt用户账号
QString g_mqttPassword;//mqtt用户密码
QString g_cameraUsername;//摄像头用户账号
QString g_cameraPassword;//摄像头用户密码
QString g_doorBaseUrl;//基本门禁url
QString g_doorAK;//门禁ak
QString g_doorSK;//门禁sk
QString g_eventDest1;//事件接收端1

void g_init()
{
    QFile file(g_configFileName);
    if(file.exists()){//如果配置文件存在则加载配置文件
        QSettings setting(g_configFileName,QSettings::IniFormat);
        setting.setIniCodec("UTF-8");

        //读取配置
        g_httpBaseUrl=setting.value("url/httpBaseUrl").toString();
        g_mqttBaseUrl=setting.value("url/mqttBaseUrl").toString();
        g_mqttUsername=setting.value("user/mqttUsername").toString();
        g_mqttPassword=setting.value("user/mqttPassword").toString();
        g_cameraUsername = setting.value("user/cameraUsername").toString();;//摄像头用户账号
        g_cameraPassword = setting.value("user/cameraPassword").toString();;//摄像头用户密码
        g_doorBaseUrl= setting.value("url/doorBaseUrl").toString();//基本门禁url
        g_doorAK= setting.value("user/doorAK").toString();//门禁ak
        g_doorSK= setting.value("user/doorSK").toString();//门禁sk
        g_eventDest1 = setting.value("url/eventDest1").toString();//事件接收端1

        //配置mqtt连接参数
        g_mqttClient.setHostname(g_mqttBaseUrl.mid(0,g_mqttBaseUrl.lastIndexOf(':')));
        g_mqttClient.setPort(g_mqttBaseUrl.mid(g_mqttBaseUrl.lastIndexOf(':')+1,g_mqttBaseUrl.size()-1).toInt());
        g_mqttClient.setUsername(g_mqttUsername);
        g_mqttClient.setPassword(g_mqttPassword);
        g_mqttClient.setProtocolVersion(QMqttClient::MQTT_5_0);
        g_mqttClient.setCleanSession(true);
        g_mqttClient.setClientId("middleware_camera");
        g_mqttClient.setKeepAlive(10);

    }else{//否则退出程序
        QMessageBox::warning(0,QString::fromLocal8Bit("警告"),QString::fromLocal8Bit("配置文件不存在,即将退出程序."));
        exit(0);
    }

}




